/*
 * @Copyright: 2023 - 2023 www.hoperun.com Inc. All rights reserved.
 */

package com.lifeservice.wxapi;

import com.lifeservice.common.core.domain.AjaxResult;
import com.lifeservice.common.utils.StringUtils;
import com.lifeservice.console.domain.LifeCityBuilding;
import com.lifeservice.console.domain.appvo.AppBaseQueryVo;
import com.lifeservice.console.mapper.LifeCityBuildingMapper;
import com.lifeservice.util.DistanceUtils;
import com.lifeservice.util.LifeServiceConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liu_yifeng
 * @function WxBuildingController
 * @date 2023/3/1 18:01
 */
@RestController
@RequestMapping("/wx/build")
@Slf4j
public class WxBuildingController {
    @Autowired
    private LifeCityBuildingMapper lifeCityBuildingMapper;

    @PostMapping("/list")
    public AjaxResult list(@RequestBody AppBaseQueryVo queryVo) {
        LifeCityBuilding query = new LifeCityBuilding();
        query.setCityName(queryVo.getCity());
        query.setStatus(LifeServiceConstants.BUILD_STATUS_ONLINE);
        List<LifeCityBuilding> dbBuildingList = lifeCityBuildingMapper.selectLifeCityBuildingList(query);
        if (StringUtils.isEmpty(dbBuildingList)) {
            return AjaxResult.success();
        }
        List<String> tags = queryVo.getTags();
        if (StringUtils.isNotEmpty(tags)) {
            // 如果用户不含0的tag，则表示是自己搜索小程序进来的，默认tag-0,这时不过滤
            if (!tags.contains(LifeServiceConstants.TAG_ALL)) {
                dbBuildingList = dbBuildingList.stream().filter(entity -> tags.contains(entity.getTag()))
                        .collect(Collectors.toList());
            }
        }
        if (StringUtils.isEmpty(queryVo.getLongitude()) || StringUtils.isEmpty(queryVo.getLatitude())) {
            return AjaxResult.success(dbBuildingList);
        }
        BigDecimal nowLongitude = new BigDecimal(queryVo.getLongitude());
        BigDecimal nowLatitude = new BigDecimal(queryVo.getLatitude());
        dbBuildingList.stream().forEach(dbBuilding -> {
            BigDecimal buildLongitude = dbBuilding.getBuildingLongitude();
            BigDecimal buildLatitude = dbBuilding.getBuildingLatitude();
            Double distance = DistanceUtils.getDistance(nowLongitude.doubleValue(), nowLatitude.doubleValue(), buildLongitude.doubleValue(), buildLatitude.doubleValue());
            dbBuilding.setDistance(distance);
            dbBuilding.setDistanceString(DistanceUtils.convertMToKm(new BigDecimal(distance)));
        });
        dbBuildingList = dbBuildingList.stream().sorted(Comparator.comparing(LifeCityBuilding::getDistance)).collect(Collectors.toList());
        return AjaxResult.success(dbBuildingList);
    }
}
